package com.yl.rss;

import java.net.URL;

import java.io.InputStream;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.io.IOException;
import com.yl.rss.PullHandler;

public class RssLoad {
	/**
	 * 解析Rss文档，获取Rss源实例
	 * @param urlString
	 * @return Object
	 */
	public RssFeed getFeed(String urlString)
	{
		try{
			PullHandler rssHandler = new PullHandler();
			InputStream inStream = this.readDataFromInternet(urlString);
			rssHandler.parseXml(inStream);  

	       return rssHandler.getFeed();
		}
		catch (Exception ee)
		{

			return null;
		}
	}
	/**
	 * 从网络上获取数据  
	 * @param urlString
	 * @return
	 */
	private InputStream readDataFromInternet(String urlString)  
	{
	    URL infoUrl = null;  
	    InputStream inStream = null;  
	    try {  
			infoUrl = new URL(urlString);  
			URLConnection connection = infoUrl.openConnection();  
			HttpURLConnection httpConnection = (HttpURLConnection)connection;  
			int responseCode = httpConnection.getResponseCode();  
			if(responseCode == HttpURLConnection.HTTP_OK){  
			    inStream = httpConnection.getInputStream();  
	        } 
	    } catch (MalformedURLException e) {
	        // TODO Auto-generated catch block  
	        e.printStackTrace();  
	    } catch (IOException e) {  
	        // TODO Auto-generated catch block  
	        e.printStackTrace();  
	    }  
	    return inStream;  
}  
}
